package com.app.common;

import com.app.utils.UtilTools;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Copyright (C) @2020 fgwang.660@gmail.com
 *
 * @author witt
 * @version 2.0
 * @className ResponseResult
 * @date 2020-06-14 13:40
 * @description 响应结果对象
 */
public class ResponseResult {

	// 响应标题
	private String title;

	// 响应类型
	private ResponseResultType type;

	// 响应内容
	private String content;

	// 等待时间
	private Float time;

	// 重定向目标
	private String targetUrl;


	/**
	 * 全功能方法
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param result    结果对象
	 * @param data      想应数据
	 * @param otherData 响应关联数据
	 * @param path      跳转路径
	 */
	public static void response(HttpServletRequest request, HttpServletResponse response, ResponseResult result, Object data, Map<String, Object> otherData, String path) {
		// 放message 对象
		if (UtilTools.isNotEmpty(result))
			request.setAttribute("MESSAGE", result);
		// 存放数据
		if(UtilTools.isNotEmpty(data))
			request.setAttribute("DATA", data);
		// 存放其它数据
		if (UtilTools.isNotEmpty(otherData))
			request.setAttribute("OTHERDATA", otherData);
		// 跳转到页面
		try {
			response.setContentType("text/html;charset=utf-8");
			request.getRequestDispatcher(path).forward(request, response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
			throw new AppException(e);
		}
	}


	/**
	 * 响应重载方法，带数据的跳转
	 *
	 * @param request  请求对象
	 * @param response 响应对象
	 * @param result   结果对象
	 * @param data     想应数据
	 * @param path     跳转路径
	 */
	public static void response(HttpServletRequest request, HttpServletResponse response, Object data, ResponseResult result, String path) {
		response(request, response, result, data, null, path);
	}

	/**
	 * 无数据的跳转页面
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param path      跳转路径
	 */
	public static void response(HttpServletRequest request,HttpServletResponse response,String path){
		response(request,response,null,path);
	}

	/**
	 * 不带其它数据  不跳转消息页面
	 *
	 * @param request  请求对象
	 * @param response 响应对象
	 * @param data     响应数据
	 * @param path     跳转路径
	 */
	public static void response(HttpServletRequest request, HttpServletResponse response, Object data, String path) {
		response(request, response, null, data, null, path);
	}

	/**
	 * 带有其他数据，但是不跳转消息页面
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param data      响应的数据
	 * @param otherData 响应的关联数据
	 * @param path      跳转路径
	 */
	public static void response(HttpServletRequest request, HttpServletResponse response, Object data, Map<String, Object> otherData, String path) {
		response(request, response, null, data, otherData, path);
	}

	/**
	 * 仅提示消息
	 *
	 * @param request  请求对象
	 * @param response 响应对象
	 * @param result   响应结果
	 * @param path     跳转页面路径
	 */
	public static void response(HttpServletRequest request, HttpServletResponse response, ResponseResult result, String path) {
		response(request, response, result, null, null, path);
	}

	/**
	 * info方法
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param title     标题
	 * @param content   内容
	 * @param time      时间
	 * @param targetUrl 目标跳转位置
	 */
	public static void info(HttpServletRequest request, HttpServletResponse response, String title, String content, Float time, Object data, String targetUrl) {
		if (UtilTools.isEmpty(title)) title = "操作提示";

		response(request, response, data, new ResponseResult()
				.setTitle(title)
				.setTime(time)
				.setType(ResponseResultType.INFO)
				.setContent(content)
				.setTargetUrl(targetUrl), "message.jsp");
	}

	/**
	 * info方法
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param title     标题
	 * @param content   内容
	 * @param time      时间
	 * @param targetUrl 目标跳转位置
	 */
	public static void info(HttpServletRequest request, HttpServletResponse response, String title, String content, Float time, String targetUrl) {
		if (UtilTools.isEmpty(title)) title = "操作提示";

		response(request, response, new ResponseResult()
				.setTitle(title)
				.setTime(time)
				.setType(ResponseResultType.INFO)
				.setContent(content)
				.setTargetUrl(targetUrl), "message.jsp");
	}

	/**
	 * 警告方法
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param title     标题
	 * @param content   内容
	 * @param time      时间
	 * @param targetUrl 目标跳转位置
	 */
	public static void warn(HttpServletRequest request, HttpServletResponse response, String title, String content, Float time, String targetUrl) {
		if (UtilTools.isEmpty(title)) title = "操作失败";

		response(request, response, new ResponseResult()
				.setTitle(title)
				.setTime(time)
				.setType(ResponseResultType.WARN)
				.setContent(content)
				.setTargetUrl(targetUrl), "message.jsp");
	}

	/**
	 * 错误方法
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param title     标题
	 * @param e         内容
	 * @param time      时间
	 * @param targetUrl 目标跳转位置
	 */
	public static void error(HttpServletRequest request, HttpServletResponse response, String title, Throwable e, Float time, String targetUrl) {
		if (UtilTools.isEmpty(title)) title = "系统消息";

		response(request, response, new ResponseResult()
				.setTitle(title)
				.setTime(time)
				.setType(ResponseResultType.ERROR)
				.setContent(e.getMessage())
				.setTargetUrl(targetUrl), "message.jsp");
	}

	/**
	 * 加载方法
	 *
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param title     标题
	 * @param content   内容
	 * @param time      时间
	 * @param targetUrl 目标跳转位置
	 */
	public static void load(HttpServletRequest request, HttpServletResponse response, String title, String content, Float time, String targetUrl) {
		if (UtilTools.isEmpty(title)) title = "页面重定向...";

		response(request, response, new ResponseResult()
				.setTitle(title)
				.setTime(time)
				.setType(ResponseResultType.LOAD)
				.setContent(content)
				.setTargetUrl(targetUrl), "message.jsp");
	}

	public String getTitle() {
		return title;
	}

	public ResponseResult setTitle(String title) {
		this.title = title;
		return this;
	}

	public ResponseResultType getType() {
		return type;
	}

	public ResponseResult setType(ResponseResultType type) {
		this.type = type;
		return this;
	}

	public String getContent() {
		return content;
	}

	public ResponseResult setContent(String content) {
		this.content = content;
		return this;
	}

	public Float getTime() {
		return UtilTools.isEmpty(time) ? 3000 : time;
	}

	public ResponseResult setTime(Float time) {
		this.time = time;
		return this;
	}

	public String getTargetUrl() {
		return targetUrl;
	}

	public ResponseResult setTargetUrl(String targetUrl) {
		this.targetUrl = targetUrl;
		return this;
	}
}

